package com.liuxin.zhiyuan.shujuzhongxin.service.impl;

import com.liuxin.zhiyuan.shujuzhongxin.mapper.ScoreSegmentMapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxin.common.utils.PageUtils;
import com.liuxin.common.utils.Query;

import com.liuxin.zhiyuan.shujuzhongxin.domain.po.ScoreSegmentEntity;
import com.liuxin.zhiyuan.shujuzhongxin.service.ScoreSegmentService;

@Service
public class ScoreSegmentServiceImpl extends ServiceImpl<ScoreSegmentMapper, ScoreSegmentEntity> implements ScoreSegmentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ScoreSegmentEntity> page = this.page(
                new Query<ScoreSegmentEntity>().getPage(params),
                new QueryWrapper<ScoreSegmentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Integer findScoreByRank(String year, String subjectType, Integer rank) {
        QueryWrapper<ScoreSegmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nianfen", year)
                   .eq("nature", subjectType)
                   .le("paiming", rank)
                   .orderByDesc("paiming")
                   .last("LIMIT 1");
        ScoreSegmentEntity result = this.getOne(queryWrapper);
        return result != null ? result.getScore() : null;
    }


    @Override
    public Integer findPaimingByScore(String year, String nature, Integer score) {
        QueryWrapper<ScoreSegmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nianfen", year)
                .eq("nature", nature)
                .eq("score", score);
        ScoreSegmentEntity result = this.getOne(queryWrapper);
        return result != null ? result.getPaiming() : null;
    }
}
